//
//  RootViewController.m
//  WeightLog
//
//  Created by Boky on 4/18/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "SettingsViewController.h"
#import "RootViewController.h"
#import "WeightHistoryViewController.h"
#import "AddNewWeightViewController.h"


@implementation RootViewController

@synthesize settingsButton, settingsViewController,peso,fecha,table, persistenceUnit;


/*
 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Custom initialization
    }
    return self;
}
*/


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	
	// add the "Save" button to the nav bar
	self.navigationItem.rightBarButtonItem = self.settingsButton;
	
	
	// Load the last weight
	peso.text=@"000.00 Unit";
	fecha.text=@"00/00/0000";
	
}


/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}


- (void)dealloc {
		
	if (self.settingsViewController != nil)
		[settingsViewController release];

	[super dealloc];

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    
	// Configure the cell.
	switch (indexPath.row) {
		case 0:
			cell.textLabel.text = @"Add new Weight";
			break;
		case 1:
			cell.textLabel.text = @"Show History";
			break;
		default:
			break;
	}
	
	
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 2;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	

	switch (indexPath.row) {
		case 1:{
			WeightHistoryViewController *weightHistoryViewController = [[WeightHistoryViewController alloc] initWithNibName:@"WeightHistoryViewController" bundle:nil];
			weightHistoryViewController.persistenceUnit = self.persistenceUnit;
			
			// Pass the selected object to the new view controller.
			[self.navigationController pushViewController:weightHistoryViewController animated:YES];
			[weightHistoryViewController release];
			break;
		}
			
		case 0:{
			AddNewWeightViewController *addNewWeightViewController = [[AddNewWeightViewController alloc] initWithNibName:@"AddNewWeightViewController" bundle:nil];
			addNewWeightViewController.persistenceUnit = self.persistenceUnit;
	
			// Pass the selected object to the new view controller.
			[self.navigationController pushViewController: addNewWeightViewController animated:YES];
			[addNewWeightViewController release];
			break;
		}
			
		default:
			break;
	}
	
	 
}

- (IBAction)settingsAction:(id)sender{

	if(self.settingsViewController == nil) // Lazy Inizialization
		self.settingsViewController = [[[SettingsViewController alloc] initWithNibName:@"SettingsViewController" bundle: nil] autorelease];
	

	[self.navigationController presentModalViewController: self.settingsViewController animated:YES];
	//[self.settingsViewController release];	 

}

@end
